﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Workflow.ComponentModel;
using System.Reflection;
using System.Configuration;

namespace Argos.Windows.Forms
{
	public partial class PropertyEditorDialog: Form
	{
		public PropertyEditorDialog()
		{
			InitializeComponent();
		}

		public PropertyEditorDialog(object selectedObject) : this()
		{
			if (null == selectedObject)
			{
				throw new ArgumentNullException("selectedObject");
			}
			this.SelectedObject = selectedObject;
		}
		


		public object SelectedObject
		{
			get { return this.PropertyGrid.SelectedObject; }
			set
			{
				if (null == value)
				{
					throw new ArgumentNullException("SelectedObject");
				}
				this.PropertyGrid.SelectedObject = value;
			}
		}



		public IDictionary<string, object> GetProperties()
		{
			return this.PropertyGrid.GetProperties();
		}

		public IDictionary<string, object> GetProperties(Predicate<PropertyInfo> propertyMatcher)
		{
			return this.PropertyGrid.GetProperties(propertyMatcher);
		}



		protected override void OnClosing(CancelEventArgs e)
		{
			base.OnClosing(e);

			if (!e.Cancel && this.WindowState == FormWindowState.Normal)
			{

				Settings.Default.FormSize = this.Size;
				Settings.Default.Save();
			}
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			this.Size = Settings.Default.FormSize;
		}



		private void OK_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
		}


		private class Settings : ApplicationSettingsBase
		{

			public static Settings Default = (Settings)ApplicationSettingsBase.Synchronized(new Settings());

			[UserScopedSetting]
			[DefaultSettingValue("400, 300")]
			public Size FormSize
			{
				get { return (Size)this["FormSize"]; }
				set { this["FormSize"] = value; }
			}
		}

	}
}
